package com.question.test;

import java.io.IOException;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException;

import com.question.dao.TopicDao;
import com.question.dao.daoFactory.DaoFactory;
import com.question.dao.daoImpl.TopicDaoImpl;
import com.question.domain.Topic;
import com.question.utils.MyProperties;

public class test {

	private static MyProperties properties = new MyProperties();
	private static TopicDao td = DaoFactory.getInstance().geTopicDao();
	
	public static String getTopicFromWord() throws IOException, XmlException, OpenXML4JException{
		String str = properties.getProperties("docxURL");
		OPCPackage opcPackage = POIXMLDocument.openPackage(str);
		POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
		String docx = ((XWPFWordExtractor) extractor).getText();
		//System.out.println(docx);
		return docx;
	}
	
	/**
	 * 将每题都分割开来
	 * 
	 * @param str
	 * @param n
	 */
	public static void splitByNumber(String str,int n){
		String[] arr = new String[2];
		Topic topic = null;
		arr[1] = str;
		int i=1;
		for(;i<=n;i++){
			arr = arr[1].split(""+i,2);
			if(i==1) continue;
			topic = deleteAndGetAnswer(arr[0]);
			topic.setQuestionId(i-1);
			td.setTopic(topic, i-1);
		}
		topic = deleteAndGetAnswer(arr[1]);
		topic.setQuestionId(i-1);
		td.setTopic(topic, i-1);
	}
	
	/**
	 * 删除题目中的答案并获取下将题目内容和答案放在一个题目对象中返回
	 * 2018年11月23日
	 * 
	 * @param str
	 * @return
	 */
	public static Topic deleteAndGetAnswer(String str){
		Topic topic = new Topic();
		String[] former = null;
		String[] last = null;
		try {
			former = str.split("（", 2);
			last= former[1].split("）", 2);
			
		} catch (Exception e) {
			former = str.split("\\(", 2);
			last= former[1].split("\\)", 2);
		}
		String answer = last[0];
		String content = former[0]+last[1];
		
		//将题目和选项分开来
		String[] arr = splitTopic(content);
		setTopic(arr, topic);
		topic.setAnswer(answer.trim().toUpperCase());
		topic.setType(isWhatType(topic));
		return topic;
	}
	
	/**
	 * 判断由arr提供的数组表示的是数目类型的题目
	 * 
	 * @param object
	 * @return 返回0表示单选，返回1表示多选，返回2表示判断
	 */
	private static int isWhatType(Topic topic) {
		if(topic.getC()==null)
			return 2;
		if(topic.getAnswer().length()>1)
			return 1;
		return 0;
	}

	/**
	 * 将字符串数组输入到题目中，字符串数组为专门匹配的数组
	 * 
	 * @param arr
	 * @param topic
	 */
	public static void setTopic(String[] arr,Topic topic){
		topic.setQuestion(arr[0]);
		topic.setA(arr[1]);
		topic.setB(arr[2]);
		topic.setC(arr[3]);
		topic.setD(arr[4]);
		topic.setE(arr[5]);
	}
	
	/**
	 * 将题目的内容和选项分割开来
	 * 
	 * @return
	 */
	public static String[] splitTopic(String topic){
		//用于存储最终的题目和选项
		String[] arr = new String[6];
		int i = 0;
		//用于暂时存储切割的元素
		String[] temp= new String[2];
		//按照该字符切割
		char ch = 'A';
		temp = topic.split(((char)(ch++))+"",2);
		arr[i++]=temp[0].trim();
		while(true) {
			try {
				temp = temp[1].split(ch+++"",2);
				arr[i++]=temp[0].trim();
			} catch (Exception e) {
				break;
			}
		}
		return arr;
	}
	
	public static void main(String[] agrs) throws IOException, XmlException, OpenXML4JException{
		String str = getTopicFromWord();
		int count = Integer.valueOf(properties.getProperties("totalTopic"));
		splitByNumber(str, count);
		/*String str="hgkjsfhgAkjalkjBjakjlCajfajhkDagkjhjdhfj";
		String[] arr = splitTopic(str);
		Topic topic = new Topic();
		setTopic(arr, topic);
		for(int i=0;i<arr.length;i++){
			System.out.println(arr[i]);
		}*/
	}
}
